﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using InterfaceXAML.Enum;
using InterfaceXAML.ModelBinding;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace InterfaceXAML.CalendarPages
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class WeekDetailPage : Page
    {
        public WeekDetailPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            InitPage();
        }

        private async void InitPage()
        {
            var date = new DateTime(Utilities.Utilities.CurrentYear,
                                        Utilities.Utilities.CurrentMonth,
                                        Utilities.Utilities.CurrentDay);
            var firstDay = date.AddDays(-Utilities.Utilities.GetRealDayOfWeek((int)date.DayOfWeek));
            var lastDay = firstDay.AddDays(6);

            week.Text = firstDay.Day + " " + ((MonthEnum)firstDay.Month) + " - " + lastDay.Day + " " + ((MonthEnum)lastDay.Month);

            Day_0.Text = "Lundi";
            Day_1.Text = "Mardi";
            Day_2.Text = "Mercredi";
            Day_3.Text = "Jeudi";
            Day_4.Text = "Vendredi";

            
            for (var i = 0; i < 5; i++)
            {
                var day = firstDay.AddDays(i);
                ((TextBlock)FindName("Day_" + i)).Text += " " + day.Day + "/" + day.Month;
            }

            var reponse = await App.ExaPlanningServiceClient.GetCalendarWeekAsync(firstDay.Year, firstDay.Month, firstDay.Day, lastDay.Year,lastDay.Month, lastDay.Day);

            var model = new WeekDetailPageModel
            {
                Projects = reponse.ProjectSet
            };

            DataContext = model;
            UpdateLayout();
        }

        private void BackButtonClick(object sender, RoutedEventArgs e)
        {
            if (Frame.CanGoBack)
            {
                Frame.GoBack();
            }
        }

        private void PrevMonthButtonClick(object sender, RoutedEventArgs e)
        {
            var date = new DateTime(Utilities.Utilities.CurrentYear, Utilities.Utilities.CurrentMonth, Utilities.Utilities.CurrentDay);
            date = date.AddDays(-7);
            Utilities.Utilities.CurrentYear = date.Year;
            Utilities.Utilities.CurrentMonth = date.Month;
            Utilities.Utilities.CurrentDay = date.Day;
            InitPage();
        }

        private void NextMonthButtonClick(object sender, RoutedEventArgs e)
        {
            var date = new DateTime(Utilities.Utilities.CurrentYear, Utilities.Utilities.CurrentMonth, Utilities.Utilities.CurrentDay);
            date = date.AddDays(7);
            Utilities.Utilities.CurrentYear = date.Year;
            Utilities.Utilities.CurrentMonth = date.Month;
            Utilities.Utilities.CurrentDay = date.Day;
            InitPage();
        }
    }
}
